//
//  ChipXiaoXiViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/26.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ChipXiaoXiViewController.h"
#import <MJRefresh.h>
#import "ChipXiaoXiDetailViewController.h"

@interface ChipXiaoXiViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSMutableArray *array;
@property (strong, nonatomic) MetaModel *meta;
@end

@implementation ChipXiaoXiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _array = [NSMutableArray array];
    
    [self setupView];
    
    [self loadData];
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:knotifi_chip paras:nil showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array addObjectsFromArray:result[@"datas"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"meta"][@"pagination"]];
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.tableview.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            weakself.meta = meta;
        }
        [weakself.tableview reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)loadMoreData {
    
    defweakself
    [HttpRequest getURL:knotifi_chip paras:@{@"page": [NSString stringWithFormat:@"%d", _meta.current_page+1]} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array addObjectsFromArray:result[@"datas"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"meta"][@"pagination"]];
        weakself.meta = meta;
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.tableview.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            [weakself.tableview.mj_footer endRefreshing];
        }
        [weakself.tableview reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        [weakself.tableview.mj_footer endRefreshing];
    }];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"预警消息";
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
    _tableview.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    _tableview.tableFooterView = [[UIView alloc] init];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 70.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *dic = _array[indexPath.row];
    cell.textLabel.text = dic[@"title"];
    cell.detailTextLabel.text = dic[@"created_at"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ChipXiaoXiDetailViewController *con = [[ChipXiaoXiDetailViewController alloc] init];
    con._id = _array[indexPath.row][@"id"];
    [self.navigationController pushViewController:con animated:YES];
}

@end
